package binarySearch;

/**
 * 给定一个按照升序排列的整数数组 nums，和一个目标值 target。
 * 找出给定目标值在数组中的开始位置和结束位置。
 * 如果数组中不存在目标值 target，返回 [-1, -1]。
 * @author le
 */
public class FindFirstAndLastIndexOfSortedArray
{
    public static void main(String[] args)
    {

    }

    public int[] searchRange(int[] nums, int target) {
        int[] res = new int[]{-1, -1};
        int n = nums.length;
        if (n == 0){
            return res;
        }
        int low = 0;
        int high = n-1;
        while(low < high){
            int mid = (low + high)/2;
            if (nums[mid] >= target){
                high = mid;
            }else{
                low = mid+1;
            }
        }
        if (nums[low] != target){
            return res;
        }else{
            res[0] = low;
            low = 0;
            high = n - 1;
            while(low < high){
                int mid = (low + high)/2;
                if (nums[mid] <= target){
                    low = mid;
                }else {
                    high = mid -1;
                }

            }
            res[1] = high;
            return res;
        }
    }
}
